// components/empty/index.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
      dayList: {
        type: Array,
        value: []
      },
    },
    /**
     * 监听属性
     */
    observers: {
      dayList(){}
    },
    /**
     * 组件的初始数据
     */
    data: {
      year: 0,
      month: 0,
      day:0,
      date: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
      dateArr: [],
      isToday: 0,
      isTodayWeek: false,
      todayIndex: 0,
      currentSelectDate: null
    },
    // 组件生命周期
    lifetimes: {
      ready() {
        let now = new Date();
        let year = now.getFullYear();
        let month = now.getMonth() + 1;
        let day = now.getDate()
        let date = this.fotmatDate(year,month,day)
        this.dateInit();
        this.setData({
          year: year,
          month: month,
          day: day,
          isToday: date,
          currentSelectDate: date
        })
      },
    },
    /**
     * 组件的方法列表
     */
    methods: {
      dateInit(setYear, setMonth) {
        // 全部时间的月份都是按0~11基准，显示月份才+1
        let dateArr = []; // 需要遍历的日历数组数据
        let arrLen = 0; // dateArr的数组长度
        let now = setYear ? new Date(setYear, setMonth) : new Date();
        let year = setYear || now.getFullYear();
        let nextYear = 0;
        let month = setMonth || now.getMonth() + 1; // 没有+1方便后面计算当月总天数
        let nextMonth = month > 11 ? 1 : month;
        let startWeek = new Date(`${year}/${month}/1`).getDay(); // 目标月1号对应的星期几
        let dayNums = new Date(year, nextMonth, 0).getDate(); // 获取目标月有多少天
        let obj = {};
        let num = 0;
        if (month > 11) {
          nextYear = year + 1;
          dayNums = new Date(nextYear, nextMonth, 0).getDate();
        }
        arrLen = startWeek + dayNums;
        for (let i = 0; i < arrLen; i++) {
          if (i >= startWeek) {
            num = i - startWeek + 1;
            let date = this.fotmatDate(year,month,num)
            obj = {
              isToday: date,
              dateNum: num
            }
          } else {
            obj = {};
          }
          dateArr[i] = obj;
        }
        this.setData({
          dateArr: dateArr
        })
        let nowDate = new Date();
        let nowYear = nowDate.getFullYear();
        let nowMonth = nowDate.getMonth() + 1;
        let nowWeek = nowDate.getDay();
        let getYear = setYear || nowYear;
        let getMonth = setMonth >= 0 ? (setMonth + 1) : nowMonth;
        if (nowYear == getYear && nowMonth == getMonth) {
          this.setData({
            isTodayWeek: true,
            todayIndex: nowWeek
          })
        } else {
          this.setData({
            isTodayWeek: false,
            todayIndex: -1
          })
        }
      },
      /**
       * 上月切换
       */
      lastMonth() {
        // 全部时间的月份都是按0~11基准，显示月份才+1
        let year = this.data.month - 2 < 0 ? this.data.year - 1 : this.data.year;
        let month = this.data.month - 2 < 0 ? 11 : this.data.month - 2;
        month = month + 1

        let date = `${year}-${month < 10 ? '0' + month : month}-01`
        let now = new Date();
        let now_year = now.getFullYear();
        let now_month = now.getMonth() + 1;
        let now_day = now.getDate()
        if(year === now_year && month === now_month) {
          date = `${year}-${month < 10 ? '0' + month : month}-${now_day < 10 ? '0' + now_day : now_day}`
        }
        this.setData({
          year: year,
          month: month,
          currentSelectDate: date
        })
        this.triggerEvent('select', {
          date: date,
        }, {})
        this.dateInit(year, month);
      },
      /**
       * 下月切换
       */
      nextMonth() {
        // 全部时间的月份都是按0~11基准，显示月份才+1
        let year = this.data.month > 11 ? this.data.year + 1 : this.data.year;
        let month = this.data.month > 11 ? 0 : this.data.month;
        month = month + 1
        let date = `${year}-${month < 10 ? '0' + month : month}-01`
        let now = new Date();
        let now_year = now.getFullYear();
        let now_month = now.getMonth() + 1;
        let now_day = now.getDate()
        if(year === now_year && month === now_month) {
          date = `${year}-${month < 10 ? '0' + month : month}-${now_day < 10 ? '0' + now_day : now_day}`
        }
        this.setData({
          year: year,
          month: month,
          currentSelectDate: date
        })
        this.triggerEvent('select', {
          date: date,
        }, {})
        this.dateInit(year, month);
      },
      /**
       * 选择日期
       */
      lookHuoDong(e){
        const {year,month,day} = e.currentTarget.dataset;
        let date = this.fotmatDate(year,month,day)
        this.setData({
          currentSelectDate: date
        })
        this.triggerEvent('select', {
          date: date,
        }, {})
      },
      // 格式化日期
      fotmatDate(year,month,day){
        return `${year}-${Number(month) < 10 ? '0' + month : month}-${Number(day) < 10 ? '0' + day : day}`
      }
    }
})